﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using PlanProd.Contracts;
using WebAppPlanProduccion.Code.Security;
using PlanProd.Contracts.Search;
using Web.Framework;

namespace WebAppPlanProduccion.App
{
    public partial class ReportServerEdit : BasePage
    {
        public ReportServer reportServerCurrent
        {
            get
            {
                return (ReportServer)ViewState["ReportServer"];
            }
            set
            {
                ViewState["ReportServer"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            HasPermissionToSeeMe(PlanProdSecurity.ADMINISTRAR_REPORTSERVER);

            if (!this.IsPostBack)
            {
                if (this.Request.QueryString["Id"] == null)
                {
                    reportServerCurrent = new ReportServer();
                }
                else
                {
                    var id = Convert.ToInt32(this.Request.QueryString["Id"]);
                    reportServerCurrent = WebServiceHelper.GetReportServers(new SearchReportServer() { Id = id }).Single();
                }
             
                Bind();
            }
        }

        protected override void ApplySecurity()
        {
        }

        private void Bind()
        {
            if (reportServerCurrent.Id != 0)
            {
                this.txtId.Text = Convert.ToString(reportServerCurrent.Id);
                this.txtUrl.Text = reportServerCurrent.Url;
                this.txtDomain.Text = reportServerCurrent.Domain;
                this.txtUserName.Text = reportServerCurrent.UserName;
                //this.txtPassword.Text = reportServerCurrent.LocaleKey;               
            }
        }

        private void SetData()
        {
            reportServerCurrent.Url = this.txtUrl.Text;
            reportServerCurrent.Domain = this.txtDomain.Text;
            reportServerCurrent.UserName = this.txtUserName.Text;
            if (!String.IsNullOrEmpty(this.txtPassword.Text))
            {
                reportServerCurrent.Password = this.txtPassword.Text;
            }
        }

        private void Save()
        {
            reportServerCurrent = WebServiceHelper.SaveReportServer(reportServerCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                BasePage.ClearReportingConfiguration(this.Application);
                Response.Redirect("/App/ReportServerList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("/App/ReportServerList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}